package cn.swust.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import cn.swust.model.InformationBean;
import cn.swust.service.InformationService;
import cn.swust.service.serviceimpl.InformationServiceImpl;
import cn.swust.util.Pager;
import cn.swust.util.PagerService;
import cn.swust.util.sendJson;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class InformationAction extends ActionSupport {
	private InformationService infoSer = new InformationServiceImpl();
	private InformationBean info = new InformationBean();
	private List<InformationBean> infoList = new ArrayList<InformationBean>();
	private int id;//信息id
	private String type;//信息类型： zero,爱心新闻，one爱心屋通知,two，鸣谢文章,three爱心心得文章,all所有
	private int count =6;
	//分页元素
	private Pager pager = new Pager();
	protected String currentPage;
	protected String pagerMethod;
	protected int totalRows;
	
	public String showInforList()
	{
		ActionContext ac=ActionContext.getContext(); 
		Map map=ac.getSession();
		if(map.get("type")==null)
			map.put("type", "zero");
		String temp = (String)map.get("type"); //取出条件
		if(type!=null&&temp!=type)
			map.put("type", type);
		else
			type =temp; //再次取出条件
		int t = 0;
		if(type.equals("zero"))
			t = 0;
		else if(type.equals("one"))
			t = 1;
		else if(type.equals("two"))
			t=2;
		else if(type.equals("three"))
			t=3;
		else t=-1;
		System.out.println("type:"+type);
		totalRows = infoSer.getCount(t);
		pager = PagerService.getPager(currentPage, pagerMethod, totalRows);
		
		infoList = infoSer.queryByType(pager, t);
		System.out.println("查询到的记录数："+infoList.size());
		return SUCCESS;
	}
	public String addInfo()
	{
		SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
		info.setTime(sim.format(new Date()));
		infoSer.addInfor(info);
		return SUCCESS;
	}
	public String delInfo()
	{
		infoSer.delInforById(id);
		return SUCCESS;
	}
	public String modInfo()
	{
		return SUCCESS;
	}
	public String queryAll()
	{
		return  SUCCESS;
	}
	///////////////////////////前台、、、、、、、、
	public String getInforJson()
	{
		System.out.println("news:"+type);
		infoList = infoSer.queryCountByType(count, Integer.valueOf(type));
		sendJson.send(ServletActionContext.getResponse(), infoList);
		return null;
	}
	public String viewInfor(){
		info = infoSer.queryById(id);
		return SUCCESS;
	}
	public InformationService getInfoSer() {
		return infoSer;
	}
	public void setInfoSer(InformationService infoSer) {
		this.infoSer = infoSer;
	}
	public InformationBean getInfo() {
		return info;
	}
	public void setInfo(InformationBean info) {
		this.info = info;
	}
	public List<InformationBean> getInfoList() {
		return infoList;
	}
	public void setInfoList(List<InformationBean> infoList) {
		this.infoList = infoList;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public Pager getPager() {
		return pager;
	}
	public void setPager(Pager pager) {
		this.pager = pager;
	}
	public String getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}
	public String getPagerMethod() {
		return pagerMethod;
	}
	public void setPagerMethod(String pagerMethod) {
		this.pagerMethod = pagerMethod;
	}
	public int getTotalRows() {
		return totalRows;
	}
	public void setTotalRows(int totalRows) {
		this.totalRows = totalRows;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
}
